package com.popcorn.notice.controller;
// 
import java.io.IOException;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.popcorn.action.Action;
import com.popcorn.action.ActionForward;
import com.popcorn.rmsystem.controller.ReservationPCheckAction;
import com.popcorn.rmsystem.controller.ReservationUpdateViewAction;

@WebServlet(name = "NoticeController",urlPatterns = "*.ao")

public class NoticeController extends javax.servlet.http.HttpServlet
      implements javax.servlet.Servlet {
   /**
    * 
    */
   private static final long serialVersionUID = 1L;

   protected void doProcess(HttpServletRequest request,HttpServletResponse response) throws ServletException, IOException {
      response.setContentType("text/html; charset=UTF-8");
      response.setCharacterEncoding("UTF-8");
      request.setCharacterEncoding("UTF-8");
      
      String RequestURI = request.getRequestURI(); 
      String contextPath = request.getContextPath();   
      String command = RequestURI.substring(contextPath.length());
      
      System.out.println("========================NoticeController======================== ");
      System.out.println("RequestURI : "+ RequestURI);
      System.out.println("contextPath : "+ contextPath);
      System.out.println("command : "+ command);
      
      ActionForward forward = null;
      Action action = null;

      if (command.equals("/notice/NInsertDB.ao")) { // 등록
         System.out.println("/notice/NInsertDB.ao");
         forward = new ActionForward();
         action = new NoticeAddAction();
         forward.setRedirect(false);
         forward.setPath("/NoticeInsert.jsp");
      }      
      else if (command.equals("/notice/NoticeListDB.ao")) { // 리스트
          System.out.println("/notice/NoticeListDB.ao");
          forward = new ActionForward();
          action = new NoticeListAction();
          forward.setRedirect(false);
          forward.setPath("/Noticeview.jsp");
      }
      else if (command.equals("/notice/NoticeUpdateViewDB.ao")) { // 수정내용
          System.out.println("/notice/NoticeUpdateViewDB.ao");
          forward = new ActionForward();
          action = new NoticeUpdateViewAction();
          forward.setRedirect(false);
          forward.setPath("/NoticeUpdate.jsp");
      }
      else if (command.equals("/notice/NoticeUpdateDB.ao")) { // 수정
          System.out.println("/notice/NoticeUpdateDB.ao");
          forward = new ActionForward();
          action = new NoticeUpdateAction();
          forward.setRedirect(false);
          forward.setPath("/NoticeDetail.jsp");
       }
       else if (command.equals("/notice/NoticeDetailDB.ao")) { // 상세보기
          System.out.println("/notice/NoticeDetailDB.ao");
          forward = new ActionForward();
          action = new NoticeDetailAction();
          forward.setRedirect(false);
          forward.setPath("/NoticeDetail.jsp");
       }
       else if (command.equals("/notice/NoticeDeleteDB.ao")) { // 삭제
           System.out.println("/notice/NoticeDeleteDB.ao");
           forward = new ActionForward();
           action = new NoticeDeleteAction();
           forward.setRedirect(false);
           forward.setPath("/Noticeview.jsp");
       }
       else if(command.equals("/notice/NoticePCheckDB.ao")){ // 비밀번호 체크
    	   System.out.println("/notice/NoticePCheckDB.ao");
    	   forward = new ActionForward();
    	   action = new NoticePCheckAction();
    	   forward.setRedirect(false);
    	   forward.setPath("/NoticeView.jsp");
       }
       else if(command.equals("/notice/NoticeSEQ_SelectDB.ao")){ // 글번호 검색
    	   System.out.println("/notice/NoticeSEQ_SelectDB.ao");
    	   forward = new ActionForward();
    	   action = new NoticeSelectAction_SEQ();
    	   forward.setRedirect(false);
    	   forward.setPath("/NoticeView.jsp");
       }
       else if(command.equals("/notice/NoticeTITLE_SelectDB.ao")){ // 제목 검색
    	   System.out.println("/notice/NoticeTITLE_SelectDB.ao");
    	   forward = new ActionForward();
    	   action = new NoticeSelectAction_TITLE();
    	   forward.setRedirect(false);
    	   forward.setPath("/NoticeView.jsp");
       }
       else if(command.equals("/notice/NoticeID_SelectDB.ao")){ // 글쓴이 검색
    	   System.out.println("/notice/NoticeID_SelectDB.ao");
    	   forward = new ActionForward();
    	   action = new NoticeSelectAction_ID();
    	   forward.setRedirect(false);
    	   forward.setPath("/NoticeView.jsp");
       }
		try{
		   forward = action.execute(request, response);
		}catch(Exception e){
		   e.printStackTrace();
		}
       
      if(forward != null){
		   if(forward.isRedirect()){
			   response.sendRedirect(forward.getPath());
		   }else{
			   RequestDispatcher dispatcher=
				   request.getRequestDispatcher(forward.getPath());
			   dispatcher.forward(request, response);
		   }
      }
   }

   protected void doGet(HttpServletRequest request,
         HttpServletResponse response) throws ServletException, IOException {
      doProcess(request, response);
   }

   protected void doPost(HttpServletRequest request,
         HttpServletResponse response) throws ServletException, IOException {
      doProcess(request, response);
   }
}